package com.xm9m.ibczy.dsp;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.google.protobuf.InvalidProtocolBufferException;
import com.xm9m.ibczy.dsp.beans.AndoIntoBean;
import com.xm9m.ibczy.dsp.beans.RTBResponseBean;
import com.xm9m.ibczy.dsp.https.HttpFactory;
import com.xm9m.ibczy.dsp.https.RTBRequestFatory;
import com.xm9m.ibczy.dsp.https.http.BCHttpListener;
import com.xm9m.ibczy.dsp.https.http.BCPostRequest;
import com.xm9m.ibczy.dsp.https.utils.AndoIntoBeanJsonFactory;
import com.xm9m.ibczy.dsp.utils.AntLog;
import com.xm9m.ibczy.dsp.views.BannerView;
import com.xm9m.ibczy.dsp.views.SingleImgView;
import com.xm9m.ibczy.dsp.views.ThreeImgView;

/**
 * Created by baichuan on 2016/11/28.
 */

public class AdvertFactory {
    //标准图文(一个120*80大小图片)
    public static final String DEFULT = "FeedsImageText_101";
    //小三图（三个120*80大小图片）
    public static final String THREE = "FeedsThreeImage_102";
    //大图(一个440*80大小图片)
    public static final String BIGIMG = "FeedsImageTitle_103";
    //(下载广告, 下载广告需要设置app_name和pka_name)
    public static final String DOWN = "FeedsAppImageTextButton_107";

    /**
     * 获得广告的视图
     * 请求网络获得数据根据不同个数据创建不同的数据并返回相应的
     *
     * @param context
     * @return
     */
    public static View getView(Context context) {
        return getViewant(context);
    }

    public static View getView2(Context context) {

        return getViewant(context);
    }

    public static View getView3(Context context) {

        return getViewant(context);
    }

    public static View getViewant(Context context) {
        FrameLayout frame = new FrameLayout(context);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        frame.setLayoutParams(params);
        getData(context, frame);
        return frame;
    }


    private static void getData(final Context context, final FrameLayout frame) {
        final BCPostRequest request = new BCPostRequest(HttpFactory.URL_ROOT);
        request.withBody(RTBRequestFatory.getRequest(context).toByteArray());
        request.withListener(new BCHttpListener() {
            @Override
            public void onSuccess(int statueCode, byte[] data) {
                try {
                    AntLog.i("code=" + statueCode + ", data=" + new String(data));
                    RTBResponseBean.RTBResponse response1 = RTBResponseBean.RTBResponse.parseFrom(data);
                    if (response1 == null)
                        return;
                    RTBResponseBean.RTBResponse.AdInfo adInfo = response1.getAdInfosList().get(0);
                    if (adInfo == null)
                        return;
                    final AndoIntoBean bean = AndoIntoBeanJsonFactory.getBean(adInfo.getJson());
                    if (bean == null)
                        return;

                    frame.post(new Runnable() {
                        @Override
                        public void run() {
                            addView(context, bean, frame);
                        }
                    });
                } catch (InvalidProtocolBufferException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(int statueCode, String message) {
                AntLog.i("code=" + statueCode + ", message=" + message);
            }
        });
        HttpFactory.addRequest(request);
    }


    private static void addView(Context context, AndoIntoBean bean, FrameLayout frame) {
        AndoIntoBean.AdsBean adsBean = bean.getAds().get(0);
        if (adsBean == null)
            return;
        // 三小图广告
        if (adsBean.getTemplate().equals(THREE)) {
            ThreeImgView threeImgView = new ThreeImgView(context);
            threeImgView.setData(bean);
            frame.removeAllViews();
            frame.addView(threeImgView);
        } else if (adsBean.getTemplate().equals(DEFULT)) {
            // 图文混合（左图右文）
            BannerView banner = new BannerView(context);
            banner.setData(bean);
            frame.removeAllViews();
            frame.addView(banner);
        } else if (adsBean.getTemplate().equals(BIGIMG)) {
            //单个大图
            SingleImgView single = new SingleImgView(context);
            single.setData(bean);
            frame.removeAllViews();
            frame.addView(single);
        }
    }
}
